package com.xci.platform.web.configuration;

import com.xci.platform.upload.IUploadFile;
import com.xci.platform.upload.WebUploadFile;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.executable.ExecutableValidator;
import java.util.Properties;

/**
 * Web项目组件配置
 */
@Configuration
public class WebBeanConfiguration {
    @Bean
    public ExecutableValidator methodValidator() {
        return beanValidator().forExecutables();
    }

    @Bean
    public Validator beanValidator() {
        return Validation
                .byProvider(HibernateValidator.class)
                .configure()
                .failFast(false)
                .buildValidatorFactory()
                .getValidator();
    }

    /**
     * 生成文件上传对象
     */
    @Bean
    public IUploadFile uploadFile() {
        return new WebUploadFile();
    }

    /**
     * 自动识别使用的数据库类型
     * 在mapper.xml中databaseId的值就是跟这里对应，
     * 如果没有databaseId选择则说明该sql适用所有数据库
     * */
    @Bean
    public DatabaseIdProvider getDatabaseIdProvider(){
        DatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties properties = new Properties();
        properties.setProperty("Oracle","oracle");
        properties.setProperty("MySQL","mysql");
        properties.setProperty("SQL Server","sqlserver");
        properties.setProperty("SQLite","sqlite");
        properties.setProperty("PostgreSQL","postgresql");
        properties.setProperty("Derby","derby");
        properties.setProperty("H2","h2");
        properties.setProperty("HSQL","hsql");
        // properties.setProperty("DB2","db2");
        // properties.setProperty("Informix","informix");
        // properties.setProperty("Sybase","sybase");
        // properties.setProperty("Hana","hana");
        databaseIdProvider.setProperties(properties);
        return databaseIdProvider;
    }
}
